//
//  LCCardTaskViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/31.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCCardTaskViewController.h"
#import "LCTaskService.h"
#import "LCTaskTableViewCell.h"

#import "LCCardTaskDetailViewController.h"

@interface LCCardTaskViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic)NSInteger page;

@property (strong, nonatomic)NSMutableArray * dataArr;

@property (nonatomic, assign) BOOL canScroll;

@property (nonatomic, assign) CGFloat sysPoint;

@end

@implementation LCCardTaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self setTableViewRefresh];
    [self getCardPoint];
    [self getCardTaskWithPage:1];
}

-(void)configUI {
    self.canScroll = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskPageControllerGoTop:) name:LCTaskPageControllerGoTopNoti object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskPageControllerLeaveTop:) name:LCTaskPageControllerLeaveTopNoti object:nil];
}

-(void)taskPageControllerGoTop:(NSNotification *)noti {
    self.canScroll = YES;
}

-(void)taskPageControllerLeaveTop:(NSNotification *)noti {
    self.canScroll = NO;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.canScroll) {
        CGFloat offsetY = scrollView.contentOffset.y;
        if (offsetY <= 0) {
            [self.tableView setContentOffset:CGPointZero];
            [[NSNotificationCenter defaultCenter] postNotificationName:LCTaskPageControllerLeaveTopNoti object:@(YES)];
        }
    } else {
        [self.tableView setContentOffset:CGPointZero];
    }
}

-(void)setTableViewRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getCardTaskWithPage:1];
    }];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getCardTaskWithPage:self.page];
    }];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    
    self.tableView.ly_emptyView = [LCEmptyView emptyViewWithImageStr:nil titleStr:nil detailStr:@"暂无数据"];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(void)getCardPoint {
    [LCTaskService getCardTaskPointWithBlock:^(BOOL isSuccess, CGFloat point) {
        self.sysPoint = point;
        [self.tableView reloadData];
    }];
}

-(void)getCardTaskWithPage:(NSInteger)page {
    
    [LCTaskService getHaoKaTaskListWithBlock:^(BOOL isSuccess, NSArray<LCCardTaskModel *> *list) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (list.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCCardTaskTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCCardTaskTableViewCell"];
    cell.sysPoint = self.sysPoint;
    cell.task = self.dataArr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LCCardTaskModel * task = self.dataArr[indexPath.row];
    
    LCCardTaskDetailViewController * detailVC = [LCCardTaskDetailViewController xx_instantiateFromStoryboardNamed:@"TaskDetail"];
    
    detailVC.sysPoint = self.sysPoint;
    detailVC.goodId = [NSString stringWithFormat:@"%ld",task.id];

    [self.parentVC.navigationController pushViewController:detailVC animated:YES];
}

@end
